﻿using System;

namespace DotNetCommon.Validate.Annotations;

/// <summary>
/// 字符串必须是手机号码(最宽松的: 11位, 以 '1' 开头即可, 另外: 兼容+区号, 如: "+8615012341234"、"+86 15012341234"), 示例:
/// <code>
/// [VMustCellPhone]
/// public string CellPhone { get; set; }
/// </code>
/// </summary>
/// <remarks>
/// 注意: 
/// <list type="bullet">
/// <item>当属性不是字符串时, 会先调用 ToString() 转为字符串再进行判断, 这可以用于对非字符串类型的校验</item>
/// <item>默认字符串不能为 null, 如果允许字符串为 null, 使用 [VMustCellPhoneIfNotNull] </item>
/// </list>
/// </remarks>
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
public sealed class VMustCellPhoneAttribute(string errorMessage = null) : Attribute, IValidateAction
{
    public string ErrorMessage { get; set; } = errorMessage;

    public void Action(IValidateContext ctx)
    {
        ValidateStringExtensions.MustCellPhoneInternal(ctx, ctx.ModelValue?.ToString(), ErrorMessage);
    }
}

/// <summary>
/// 如果不为null, 则字符串必须是手机号码(最宽松的: 11位, 以 '1' 开头即可, 另外: 兼容+区号, 如: "+8615012341234"、"+86 15012341234"), 示例:
/// <code>
/// [VMustCellPhoneIfNotNull]
/// public string CellPhone { get; set; }
/// </code>
/// </summary>
/// <remarks>
/// 注意: 
/// <list type="bullet">
/// <item>当属性不是字符串时, 会先调用 ToString() 转为字符串再进行判断, 这可以用于对非字符串类型的校验</item>
/// </list>
/// </remarks>
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
public sealed class VMustCellPhoneIfNotNullAttribute(string errorMessage = null) : Attribute, IValidateAction
{
    public string ErrorMessage { get; set; } = errorMessage;

    public void Action(IValidateContext ctx)
    {
        if (ctx.ModelValue == null) return;
        ValidateStringExtensions.MustCellPhoneInternal(ctx, ctx.ModelValue?.ToString(), ErrorMessage);
    }
}
